<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="uid">
        <el-select v-model="queryParams.uid" placeholder="请选择用户" style="width: 200px;" @keyup.enter="handleQuery">
          <el-option v-for="user in usersList" :key="user.id" :label="user.userName" :value="user.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="queryParams.idCard" placeholder="请输入身份证号" clearable @keyup.enter="handleQuery" style="width:200px;"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['gas:user_verification:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['gas:user_verification:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['gas:user_verification:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['gas:user_verification:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="user_verificationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" fixed="left"/>
      <el-table-column label="姓名" align="center" prop="uid">
        <template #default="scope">
          {{ getUserNameById(scope.row.uid) }}
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="民族" align="center" prop="nation" />
      <el-table-column label="生日" align="center" prop="birthday" width="100" />
      <el-table-column label="家庭住址" align="center" width="100">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="showAddressDetail(scope.row.address)">查看详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" width="135">
        <template #default="scope">
          <!-- 调用 maskIdCard 函数处理身份证号 -->
          {{ maskIdCard(scope.row.idCard) }}
        </template>
      </el-table-column>
      <el-table-column label="证件正面" align="center" prop="front" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.front" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="证件背面" align="center" prop="opposite" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.opposite" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="比对分数" align="center" prop="score" />
      <el-table-column label="人脸图片" align="center" prop="faceImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.faceImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="130" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['gas:user_verification:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['gas:user_verification:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="showAddressDialog" title="家庭住址详情" width="30%">
      <p>{{ addressDetail }}</p>
    </el-dialog>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改实名认证对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="user_verificationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名称" prop="uid">
          <el-select v-model="form.uid" placeholder="请选择用户" @change="fillUserName">
            <el-option v-for="user in usersList" :key="user.id" :label="user.userName" :value="user.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="自动填充姓名" disabled />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option v-for="dict in sys_user_sex" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-input v-model="form.birthday" placeholder="请输入生日" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="证件正面" prop="front">
          <image-upload v-model="form.front" />
        </el-form-item>
        <el-form-item label="证件背面" prop="opposite">
          <image-upload v-model="form.opposite" />
        </el-form-item>
        <el-form-item label="比对分数" prop="score">
          <el-input v-model="form.score" placeholder="请输入人脸比对分数" />
        </el-form-item>
        <el-form-item label="人脸图片" prop="faceImg">
          <image-upload v-model="form.faceImg" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="User_verification">
import { ref } from 'vue'
import { listUser_verification, getUser_verification, delUser_verification, addUser_verification, updateUser_verification } from "@/api/gas/user_verification"
import { listUsers } from "@/api/gas/users"

const { proxy } = getCurrentInstance()
const { sys_user_sex } = proxy.useDict('sys_user_sex')

const user_verificationList = ref([])
const usersList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    uid: null,
    idCard: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)
// 控制对话框显示隐藏
const showAddressDialog = ref(false)
// 存储家庭住址详情
const addressDetail = ref('')

// 显示家庭住址详情的函数
function showAddressDetail(address) {
  addressDetail.value = address
  showAddressDialog.value = true
}
/** 查询实名认证列表 */
function getList() {
  loading.value = true
  listUser_verification(queryParams.value).then(response => {
    user_verificationList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
function getListUsers() {
  loading.value = true
  queryParams.value.params = {}
  listUsers(queryParams.value).then(response => {
    usersList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
function getUserNameById(userId) {
  const user = usersList.value.find(user => user.id == userId)
  return user ? user.userName : '未知用户'
}
function maskIdCard(idCard) {
  if (!idCard) return ''
  // 使用正则表达式将第 4 位到第 16 位替换为星号
  return idCard.replace(/^(.{3}).{13}(.*)$/, '$1*************$2')
}
// 取消按钮
function cancel() {
  open.value = false
  reset()
}
function fillUserName(userId) {
  const user = usersList.value.find(user => user.id == userId)
  if (user) {
    form.value.name = user.userName
  } else {
    form.value.name = ''
  }
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    uid: null,
    name: null,
    sex: null,
    nation: null,
    birthday: null,
    address: null,
    idCard: null,
    front: null,
    opposite: null,
    score: null,
    faceImg: null
  }
  proxy.resetForm("user_verificationRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加实名认证"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getUser_verification(_id).then(response => {
    form.value = response.data
    // 触发填充姓名
    fillUserName(form.value.uid)
    open.value = true
    title.value = "修改实名认证"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["user_verificationRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateUser_verification(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addUser_verification(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除实名认证编号为"' + _ids + '"的数据项？').then(function () {
    return delUser_verification(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('gas/user_verification/export', {
    ...queryParams.value
  }, `user_verification_${new Date().getTime()}.xlsx`)
}

getList()
getListUsers()
</script>
